Skip to content

Make the CLI mode available via the SAPI globals #14479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2024

Conversation

TimWolla
Copy link
Member

@TimWolla TimWolla commented Jun 5, 2024

When hooking into RINIT it is currently pretty much impossible to determine whether a file will actually be executed or if it just will be linted, highlighted, or comments stripped: The startup is identical for all of them and the chosen mode is not currently exposed to other extensions.

The SG(server_context) is currently entirely unused for the cli SAPI. It appears to be appropriate to store the mode as a SAPI-specific information inside of it.

TimWolla added 2 commits June 5, 2024 12:39
When hooking into RINIT it is currently pretty much impossible to determine
whether a file will actually be executed or if it just will be linted,
highlighted, or comments stripped: The startup is identical for all of them and
the chosen mode is not currently exposed to other extensions.

The `SG(server_context)` is currently entirely unused for the `cli` SAPI. It
appears to be appropriate to store the mode as a SAPI-specific information
inside of it.
@TimWolla TimWolla requested review from nielsdos and Girgias June 5, 2024 10:57
@nielsdos
Copy link
Member

nielsdos commented Jun 5, 2024

The patch seems right, but I wonder why you need to know this in RINIT?

@TimWolla
Copy link
Member Author

TimWolla commented Jun 6, 2024

but I wonder why you need to know this in RINIT?

My use case includes a fairly expensive and ultimately useless startup phase if no script is actually executed.

In fact such a use case also exists within PHP itself: ext/session when combined with session.auto_start=1. It will perform all the session startup logic, including the creation of a new session, even when just syntax checking a file:

$ strace -eopenat sapi/cli/php -d session.auto_start=1 -l test.php 2>|grep sess_
openat(AT_FDCWD, "/tmp/sess_92830e591ed498cc8897b62d18c1dcab", O_RDWR|O_CREAT|O_NOFOLLOW, 0600) = 4
No syntax errors detected in test.php

Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me, would possibly be interesting to improve the session extension to not setup stuff when linting.

param_error = "Source stripping only works for files.\n";
break;
}
behavior=PHP_MODE_STRIP;
context.mode=PHP_CLI_MODE_STRIP;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits: while at it, could you add spaces around = ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do this in a follow-up PR. Looking at the function there are more issues than the missing space for context.mode and I want to keep git blame useful.

@nielsdos
Copy link
Member

nielsdos commented Jun 6, 2024

Okay I see. However in that case I don't think this patch is entirely right. The server mode of the CLi SAPI does use server_context to store client data. How is your RINIT code gonna know which data structure is stored in the context?

@TimWolla
Copy link
Member Author

TimWolla commented Jun 6, 2024

The CLI Server uses cli-server as the SAPI name:

"cli-server", /* name */

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay then this is fine

@withinboredom
Copy link
Member

Will this need a docs update about cli mode? It's already quite special: https://www.php.net/manual/en/features.commandline.differences.php

@Girgias
Copy link
Member

Girgias commented Jun 6, 2024

Will this need a docs update about cli mode? It's already quite special: https://www.php.net/manual/en/features.commandline.differences.php

I don't see what the docs have to do with this change? This is an internal change to the SAPI, the docs cover userland.

@withinboredom
Copy link
Member

I don't see what the docs have to do with this change? This is an internal change to the SAPI, the docs cover userland.

@TimWolla said:

In fact such a use case also exists within PHP itself: ext/session when combined with session.auto_start=1. It will perform all the session startup logic, including the creation of a new session, even when just syntax checking a file

Until I looked at the diff a little deeper, I thought this would affect that now. In other words, if someone were relying on that kind of behavior, it might break.

@TimWolla TimWolla merged commit bca0c08 into php:master Jun 10, 2024
11 checks passed
@TimWolla TimWolla deleted the cli-mode-expose branch June 10, 2024 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants